<!--
 * @Description: 
 * @Version: 1.0
 * @Autor: younghxp
 * @Date: 2022-03-14 14:36:06
 * @LastEditors: younghxp
 * @LastEditTime: 2022-03-28 18:07:50
-->
<template>
  <div class="container" ref="mainCon">
    <div class="mainCon">
      <el-row class="searchBox searchInputWidth">
        <el-form :inline="true" :model="filterField" ref="accountForm">
          <el-col :lg="8" :xl="6">
            <el-form-item label="项目名称" class="row-padding-bottom">
              <el-input
                placeholder="请输入项目名称"
                clearable
                v-model="filterField.projectName"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="默认类型" class="row-padding-bottom">
              <el-select
                v-model="filterField.projectWhiteType"
                placeholder="请选择"
                clearable
              >
                <el-option
                  label="普通项目"
                  value="ORDINARY_PROJECT"
                ></el-option>
                <el-option label="小项目" value="MINI_PROJECT"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-form-item>
            <el-button
              type="primary"
              icon="icon iconfont iconchazhao1"
              @click="searchList"
              class="searchBtn"
            >
              <span>搜索</span>
            </el-button>
            <el-button
              v-hasBtn="'smallProjectSet:add'"
              class="commonBut saveBtn funComBut addSecondTab"
              icon="icon iconfont icontianjia"
              @click="addWhiteList"
            >
              <span class="opetareText">添加白名单</span>
            </el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <!-- 列表 -->
        <el-table
          ref="tableBox1"
          @sort-change="sortChange"
          :max-height="tableHeight"
          :data="tableData"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          v-loading="loading"
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column prop="projectName" label="项目名称" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.projectName"
                :columnName="'projectName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="projectWhiteType"
            label="默认类型"
            min-width="160"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column prop="reason" label="列入名单原因" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.projectName"
                :columnName="'projectName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="createTime"
            label="添加时间"
            min-width="180"
            sortable="custom"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column align="left" label="操作" fixed="right" width="180">
            <template slot-scope="scope">
              <el-link
                type="primary"
                v-hasBtn="'smallProjectSet:update'"
                @click="editWhiteList(scope.row.id)"
                >修改</el-link
              >
              <el-link
                type="primary"
                v-hasBtn="'smallProjectSet:delete'"
                @click="delConfirm(scope.row.id)"
                >移出</el-link
              >
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息"></emptyTable>
          </div>
        </el-table>
        <!-- filterField为搜索条件 -->
        <Pagination
          ref="pagination"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
          @getpageNo="getpageNo"
          @getpageSize="getpageSize"
        ></Pagination>
      </div>
    </div>
    <whiteAdd
      v-if="dialogVisible"
      :dialogVisible="dialogVisible"
      :id="id"
      :dialogType="dialogType"
      @closeDialog="closeDialog"
    ></whiteAdd>
  </div>
</template>

<script>
import whiteAdd from './whiteAdd'
import scrollTable from '@/mixin/scrollTable.js'

export default {
  name: 'WhiteList',
  mixins: [scrollTable],
  components: {
    whiteAdd,
  },
  data() {
    return {
      requestUrl: '/api-p/smallprojectwhite/getListByPage',
      filterField: {
        projectName: '',
        projectWhiteType: '',
      },
      id: '',
      pageSize: 20,
      pageNo: 1,
      tableData: [], //表格数据
      loading: true,
      isShow: false,
      dialogVisible: false,
      whiteListDetail: {},
      dialogType: '',
    }
  },
  props: {
    index: {
      type: String,
    },
  },
  watch: {
    index(newVal) {
      if (newVal === '1') {
        this.$refs.tableBox1 && this.$refs.tableBox1.doLayout()
      }
    },
  },
  created() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
  },
  activated() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
    })
  },
  methods: {
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    getTableList(data) {
      this.tableData = data
      this.loading = false
      if (this.tableData.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      this.pageSize = 20
      this.pageNo = 1
      this.$refs.pagination.getTableListData()
    },
    closeDialog(val) {
      this.dialogVisible = false
      if (val === 'update') {
        this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
      } else {
        this.searchList()
      }
    },
    // 添加
    addWhiteList() {
      this.dialogType = 'add'
      this.dialogVisible = true
    },
    // 修改
    editWhiteList(id) {
      this.dialogType = 'edit'
      this.id = id
      this.dialogVisible = true
    },
    // 二次确认
    delConfirm(id) {
      this.id = id
      this.$confirm('请确认是否删除该条信息？', '提示', {
        cancelButtonClass: 'btn-custom-cancel',
        type: 'warning',
      })
        .then(() => {
          this.delWhiteList()
        })
        .catch(() => {
          // this.$message.error("删除失败");
        })
    },
    // 删除
    delWhiteList() {
      return this.axios({
        method: 'post',
        url: '/api-p/smallprojectwhite/removeWhite',
        data: {
          id: this.id,
        },
      })
        .then((response) => {
          if (!response.data.hasError) {
            this.$message.success('删除成功')
            this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
          } else {
            this.$message.error(response.data.errorMsg)
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
  },
}
</script>

<style scoped lang="less">
@import '../../../static/css/filterArea.less';
.mainCon {
  top: 213px;
}
</style>
